/*
 * Copyright (c) 2007 jNetX.
 * http://www.jnetx.com
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * jNetX. You shall not disclose such Confidential Information and
 * shall use it only in accordance with the terms of the license
 * agreement you entered into with jNetX.
 *
 * $Id$
 */
package com.jnetx.javax.sip.util;

import java.text.ParseException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * @author <a href="mailto:dparhonin@jnetx.ru">Dmitry Parhonin</a>
 * @version $Revision$
 */
public final class Parameterized<T> implements Cloneable {
    private final T object;
    private final Map<String, String> parameters = new HashMap<String, String>();

    public Parameterized(T object) {
        this.object = object;
    }

    public T getObject() {
        return object;
    }

    public Map<String, String> getParameters() {
        return parameters;
    }

    public void putParameters(Map<String, String> source) {
        parameters.putAll(source);
    }

    public final String getParameter(String name) {
        return parameters.get(name);
    }

    public final void setParameter(String name, String value) throws ParseException {
        parameters.put(name, value);
    }

    public final Iterator getParameterNames() {
        return parameters.keySet().iterator();
    }

    public final void removeParameter(String name) {
        parameters.remove(name);
    }

    @Override
    public Object clone() throws CloneNotSupportedException {
        final Parameterized<T> clone = new Parameterized<T>(this.object);
        clone.parameters.putAll(this.parameters);
        return clone;
    }

    public StringBuilder getParametersAsString(StringBuilder sb) {
        if (parameters.size() != 0) {
            final String delimiter = ";";
            for (Map.Entry<String, String> entry : parameters.entrySet()) {
                final String name = entry.getKey();
                final String value = entry.getValue();
                sb.append(delimiter).append(name).append('=').append(value);
            }
        }
        return sb;
    }

    public String toString() {
        final StringBuilder sb = new StringBuilder();
        sb.append(object);
        return getParametersAsString(sb).toString();
    }
}
/*
 * $Log$
 */